var username = document.querySelector('.username');
var age = document.querySelector('.age');
var identity = document.querySelector('.identity');
var nickname = document.querySelector('.nickname');
var gender = document.querySelector('.gender');
var createTime = document.querySelector('.createTime');
var updateTime = document.querySelector('.updateTime');

sendAjax({
    url: 'http://localhost:8888/users/info',
    headers: {
        authorization: localStorage.getItem('token')
    },
    data: {
        id: getCookie('userId')
    },
    success: res => {
        console.log(res)
        username.nextElementSibling.innerText = res.info.username
        age.nextElementSibling.innerText = res.info.age
        gender.nextElementSibling.innerText = res.info.gender
        nickname.nextElementSibling.innerText = res.info.nickname

        identity.nextElementSibling.innerText = res.info.identity

        var date = new Date(res.info.createTime)
        console.log(date);
        var year = date.getFullYear()
        var month = date.getMonth() + 1
        var day = date.getDate()
        var hour = date.getHours()
        var minute = date.getMinutes()
        var second = date.getSeconds()
        createTime.nextElementSibling.innerText = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second

        var date = new Date(res.info.updateTime)
        var year = date.getFullYear()
        var month = date.getMonth() + 1
        var day = date.getDate()
        var hour = date.getHours()
        var minute = date.getMinutes()
        var second = date.getSeconds()
        updateTime.nextElementSibling.innerText = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second
        // 修改
        edit()
    }
})
// 修改
function edit() {

    var btn = document.querySelector('[type="button"]');
    btn.onclick = function () {
        if (this.value === '修改') {
            this.value = '提交'
            console.log('点击了修改');

            var input = document.createElement('input')
            // input.value = age.innerText
            age.nextElementSibling.innerHTML = ''
            age.nextElementSibling.appendChild(input)

            // 性别
            var input = document.createElement('input')
            // input.value = gender.innerText
            gender.nextElementSibling.innerHTML = ''
            gender.nextElementSibling.appendChild(input)

            // 昵称
            var input = document.createElement('input')
            // input.value = nickname.innerText
            nickname.nextElementSibling.innerHTML = ''
            nickname.nextElementSibling.appendChild(input)
        } else if (this.value === '提交') {
            this.value = '修改'
            console.log('点击了提交');
            var age1 = age.nextElementSibling.firstElementChild.value
            var gender1 = gender.nextElementSibling.firstElementChild.value
            var nickname1 = nickname.nextElementSibling.firstElementChild.value
            // 发送ajax请求
            sendAjax({
                url: 'http://localhost:8888/users/update',
                method: 'post',
                headers: {
                    authorization: localStorage.getItem('token')
                },
                data: {
                    id: getCookie('userId'),
                    age: age1,
                    gender: gender1,
                    nickname: nickname1
                },
                success: res => {
                    console.log(res);
                    alert(res.message)
                    age.nextElementSibling.innerHTML = age1;
                    gender.nextElementSibling.innerHTML = gender1;
                    nickname.nextElementSibling.innerHTML = nickname1;
                    
                }

            })
        }
    }
}